class EventsUsersController < ApplicationController
  def index
    @event = Event.find(params[:event_id])
    @conventioners = @event.conventioners.paginate :page => params[:page], :per_page =>10
#    unless (params[:page])
#      page.replace_html "show_users",:partial=>'events_users/user_list',:locals=>{:conventioners=>@conventioners}
#    else
#      render :update do |page|
#        page.replace_html "show_users",:partial=>'events_users/user_list',:locals=>{:conventioners=>@conventioners,:page=>params[:page]}
#      end
#    end
  end

  def create
    @user = User.new(params[:user])
    @user.save
    event = Event.find(params[:event_id])
    @conventioner = @user.conventioners.build(:event=>event)
    @conventioner.category = params[:conventioner][:category]
    @conventioner.is_leader = params[:conventioner][:is_leader]
    if params[:conventioner][:revert]
      @conventioner.revert_id = params[:conventioner][:revert_id]
    end
    if params[:conventioner][:arrive]
      @conventioner.arrive_id = params[:conventioner][:arrive_id]
    end
    @conventioner.save
  end

  def show_edit
    @conventioner = Conventioner.find(params[:id])
    render :partial=>'events_users/edit_user_form',:locals=>{:conventioner=>@conventioner}
  end

  def hide_edit
    render :text=>'';
  end

  def update
    @conventioner = Conventioner.find(params[:id])
    user=@conventioner.user
    user.update_attributes(params[:user])
    @conventioner.category = params[:conventioner][:category]
    @conventioner.is_leader = params[:conventioner][:is_leader]
    if params[:conventioner][:revert]
      @conventioner.revert_id = params[:conventioner][:revert_id]
    end
    if params[:conventioner][:arrive]
      @conventioner.arrive_id = params[:conventioner][:arrive_id]
    end
    if @conventioner.save
      render :update do |page|
        page.replace "#{@conventioner.id}", :partial=>"events_users/user_item",:locals=>{:conventioner=>@conventioner}
        page[@conventioner.id.to_s].visual_effect :highlight
        page.replace_html("edit_place_#{@conventioner.id}", '')
      end
    end
  end

  def destroy
    @conventioner = Conventioner.find(params[:id])
    if @conventioner.destroy
      render :update do |page|
        page.remove("#{@conventioner.user.id}")
      end
    end
  end

end